package com.shuai.utils.data;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
import com.baomidou.mybatisplus.core.toolkit.support.ColumnCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 数据库Util
 *
 * @author shuai
 * @since 2022
 */
@Slf4j
public class DataUtil {

    public DataUtil() {
    }

    public static void fillOrderBy(Object query, QueryWrapper<?> queryWrapper, Class<?> aClass) {
        if (query != null) {
            String orderByProperties = BeanUtil.getProperty(query, "orderBy").toString();
            String order = BeanUtil.getProperty(query, "order").toString();
            if (!StrUtil.isEmpty(orderByProperties)) {
                boolean isAsc = StrUtil.equals("ASC", order, true);
                fillOrderBy(queryWrapper, aClass, isAsc, orderByProperties.split(","));
            }
        }
    }

//    public static void fillOrderBy(Object query, OrderByEnumFunction orderEnum, QueryWrapper<?> queryWrapper, Class<?> aClass) {
//        if (query != null) {
//            if (orderEnum != null && orderEnum.apply() != null) {
//                String orderByProperties = BeanUtil.getProperty(query, "orderBy").toString();
//                String order = BeanUtil.getProperty(query, "order").toString();
//                if (!StrUtil.isEmpty(orderByProperties)) {
//                    HashMap<String, String> mapping = orderEnum.apply();
//                    String[] orderByPropertyArray = orderByProperties.split(",");
//
//                    for(int i = 0; i < orderByPropertyArray.length; ++i) {
//                        String mappingProperty = (String)mapping.get(orderByPropertyArray[i]);
//                        if (StrUtil.isNotEmpty(mappingProperty)) {
//                            orderByPropertyArray[i] = mappingProperty;
//                        }
//                    }
//
//                    boolean isAsc = StrUtil.equals("ASC", order, true);
//                    fillOrderBy(queryWrapper, aClass, isAsc, orderByPropertyArray);
//                }
//            } else {
//                fillOrderBy(query, queryWrapper, aClass);
//            }
//        }
//    }

    public static void fillOrderBy(QueryWrapper<?> queryWrapper, Class<?> aClass, boolean isAsc, String... orderByProperties) {
        if (orderByProperties != null && orderByProperties.length != 0) {
            List<String> orderByColumns = new ArrayList();
            String[] orderBys = orderByProperties;
            int len = orderByProperties.length;

            for(int i = 0; i < len; i++) {
                String orderByProperty = orderBys[i];
                String orderByColumn = getColumnByProperty(aClass, orderByProperty);
                if (StrUtil.isNotEmpty(orderByColumn)) {
                    orderByColumns.add(orderByColumn);
                }
            }

            if (!CollUtil.isEmpty(orderByColumns)) {
                queryWrapper.orderBy(true, isAsc, new String[]{CollUtil.join(orderByColumns, ",")});
            }
        }
    }

    public static void fillOrderBy(QueryWrapper<?> queryWrapper, boolean isAsc, String... orderByProperties) {
        if (orderByProperties != null && orderByProperties.length != 0) {
            List<String> orderByColumns = new ArrayList();
            String[] var4 = orderByProperties;
            int var5 = orderByProperties.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                String orderByProperty = var4[var6];
                String orderByColumn = StrUtil.toUnderlineCase(orderByProperty);
                orderByColumns.add(orderByColumn);
            }

            queryWrapper.orderBy(true, isAsc, new String[]{CollUtil.join(orderByColumns, ",")});
        }
    }

    public static String getColumnByProperty(Class<?> clazz, String property) {
        String column = null;
        Map<String, ColumnCache> columnCacheMap = LambdaUtils.getColumnMap(clazz);
        if (StringUtils.isNotEmpty(property)) {
            ColumnCache columnCache = (ColumnCache)columnCacheMap.get(property.toUpperCase());
            if (columnCache != null) {
                column = columnCache.getColumn();
            }
        }

        return column;
    }

}